

//去空格
String.prototype.Trim = function () {
    var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}



//显示和隐藏导航栏
function nav(obj) {
    var obj = $(obj);
    var name = obj.attr('name');
    var cur_class = obj.attr('class');
    if (cur_class == 'open') {
        obj.removeClass();
        obj.addClass('close');
        $('#' + name).slideUp('fast');
    } else {
        obj.removeClass();
        obj.addClass('open');
        $('#' + name).slideDown('fast');
    }


}

//导航栏
function showMenu(obj) {
    if (document.getElementById("showid")) {
        document.getElementById("showid").id = "_xx";
    }
    obj.id = "showid";
}
function CloseMenu() {

    if (document.getElementById("showid")) {
        document.getElementById("showid").id = "_xx";
    }

}



/////////首页js////////////
//导航栏
function showMenu(obj) {
    if (document.getElementById("showid")) {
        document.getElementById("showid").id = "_xx";
    }
    obj.id = "showid";
}
function CloseMenu() {

    if (document.getElementById("showid")) {
        document.getElementById("showid").id = "_xx";
    }

}

//幻灯片
var $x = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
    create: function () {
        return function () {
            this.initialize.apply(this, arguments);
        }
    }
}

Object.extend = function (destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}

var TransformView = Class.create();
TransformView.prototype = {
    //容器对象,滑动对象,切换参数,切换数量
    initialize: function (container, slider, parameter, count, options) {
        if (parameter <= 0 || count <= 0) return;
        var oContainer = $x(container), oSlider = $x(slider), oThis = this;

        this.Index = 0; //当前索引

        this._timer = null; //定时器
        this._slider = oSlider; //滑动对象
        this._parameter = parameter; //切换参数
        this._count = count || 0; //切换数量
        this._target = 0; //目标参数

        this.SetOptions(options);

        this.Up = !!this.options.Up;
        this.Step = Math.abs(this.options.Step);
        this.Time = Math.abs(this.options.Time);
        this.Auto = !!this.options.Auto;
        this.Pause = Math.abs(this.options.Pause);
        this.onStart = this.options.onStart;
        this.onFinish = this.options.onFinish;
        try {
            oContainer.style.overflow = "hidden";
            oContainer.style.position = "relative";


            oSlider.style.position = "absolute";
            oSlider.style.top = oSlider.style.left = 0;
        } catch (err) { }
    },
    //设置默认属性
    SetOptions: function (options) {
        this.options = {//默认值
            Up: true, //是否向上(否则向左)
            Step: 5, //滑动变化率
            Time: 10, //滑动延时
            Auto: true, //是否自动转换
            Pause: 2000, //停顿时间(Auto为true时有效)
            onStart: function () { }, //开始转换时执行
            onFinish: function () { } //完成转换时执行
        };
        Object.extend(this.options, options || {});
    },
    //开始切换设置
    Start: function () {
        if (this.Index < 0) {
            this.Index = this._count - 1;
        } else if (this.Index >= this._count) { this.Index = 0; }

        this._target = -1 * this._parameter * this.Index;
        this.onStart();
        this.Move();
    },
    //移动
    Move: function () {
        clearTimeout(this._timer);
        try {
            var oThis = this, style = this.Up ? "top" : "left", iNow = parseInt(this._slider.style[style]) || 0, iStep = this.GetStep(this._target, iNow);

            if (iStep != 0) {
                this._slider.style[style] = (iNow + iStep) + "px";
                this._timer = setTimeout(function () { oThis.Move(); }, this.Time);
            } else {
                this._slider.style[style] = this._target + "px";
                this.onFinish();
                if (this.Auto) { this._timer = setTimeout(function () { oThis.Index++; oThis.Start(); }, this.Pause); }
            }
        } catch (err) { }
    },
    //获取步长
    GetStep: function (iTarget, iNow) {
        var iStep = (iTarget - iNow) / this.Step;
        if (iStep == 0) return 0;
        if (Math.abs(iStep) < 1) return (iStep > 0 ? 1 : -1);
        return iStep;
    },
    //停止
    Stop: function (iTarget, iNow) {
        clearTimeout(this._timer);
        this._slider.style[this.Up ? "top" : "left"] = this._target + "px";
    }
};

window.onload = function () {
    function Each(list, fun) {
        for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
    };

    var objs = $("#idNum").children("li");

    var tv = new TransformView("idTransformView", "idSlider", 330, 4, {
        onStart: function () { Each(objs, function (o, i) { o.className = tv.Index == i ? "on" : ""; }) } //按钮样式
    });

    tv.Start();

    Each(objs, function (o, i) {
        o.onmouseover = function () {
            o.className = "on";
            tv.Auto = false;
            tv.Index = i;
            tv.Start();
        }
        o.onmouseout = function () {
            o.className = "";
            tv.Auto = true;
            tv.Start();
        }
    })

}



//精选商品  /精选店铺 /最新商品
jQuery(function ($) {
    //rec_list

    var rec_list = $('#rec_list'),
	recUl = rec_list.find('.inner ul').eq(0),
	slider = new Slider(
    {
        shell: recUl,
        length: Math.ceil(recUl.find('li').length / 4),
        auto: false,
        duration: 320,
        unitSize: 648
    });
    rec_list.find('a.prev,a.next').click(function (e) {
        e.preventDefault();
        slider[this.className.indexOf('prev') > -1 ? 'prev' : 'next']();
    });

    //hot_sells
    var 
	hotShell = $('#good_shops'),
	hotUl = hotShell.find('.inner ul').eq(0),
	hotSlider = new Slider({
	    shell: hotUl,
	    length: Math.ceil(hotUl.find('li').length / 5),
	    auto: false,
	    duration: 400,
	    unitSize: 900
	});
    hotShell.find('a.prev,a.next').click(function (e) {
        e.preventDefault();
        hotSlider[this.className.indexOf('prev') > -1 ? 'prev' : 'next']();
    });
    var lastShell = $('#last_goods'),
	lastUl = lastShell.find('.inner ul').eq(0),
	lastSlider = new Slider(
    {
        shell: lastUl,
        length: Math.ceil(lastUl.find('li').length / 5),
        auto: false,
        duration: 400,
        unitSize: 900
    });
    lastShell.find('a.prev,a.next').click(function (e) {
        e.preventDefault();
        lastSlider[this.className.indexOf('prev') > -1 ? 'prev' : 'next']();
    });
});
/*
-- 基于jQuery(1.7)扩展插件 Slider --
author : kongge
create : 2011.12.13
update : 2012.03.28
-- 接口说明 --
- @Param options {Object} #2011.12.05#
- {
length : 3,
shell : null,
auto : false,
cName : 'left',
duration : 200,
unitSize : 500,
delay : 5000,
onbeforeplay : _noop,	//此时如果return false; 将会停止此次动画
onplay : _noop,
ondone : _noop
}
-- 接口说明 end --
*/
; (function () {
    var 
	_noop = function () { },
	slider =
	window.Slider = function (ops) {
	    this.init(ops || {});
	};
    slider.prototype = {
        constructor: slider,
        _ops: {
            length: 4,
            shell: null,
            auto: false,
            autoStop: true,
            cName: 'left',
            duration: 200,
            unitSize: 500,
            delay: 5000,
            easing: 'swing',
            onplay: _noop,
            ondone: _noop,
            onbeforeplay: _noop
        },
        init: function (ops) {
            var 
			k,
			self = this,
			_ops = this._ops;
            for (k in _ops) {
                if (typeof ops[k] === 'undefined') {
                    ops[k] = _ops[k];
                }
            }
            this.index = 0;
            this.ops = ops;
            if (ops.auto) {
                this.autoPlay();
                ops.autoStop && ops.shell.hover(function () {
                    self.stopAuto();
                }, function () {
                    self.autoPlay();
                });
            }
        },
        play: function (inx) {
            inx = parseInt(inx, 10);
            inx = isFinite(inx) ? inx : this.index + 1;
            var 
			ani = {},
			self = this,
			ops = this.ops,
			shell = ops.shell,
			len = ops.length;
            inx = (inx < 0 ? len - 1 : inx) % len;
            if (!shell || !shell.animate || inx === this.index || ops.onbeforeplay.call(this, inx, this.index) === false) return this;
            ani[ops.cName] = -inx * ops.unitSize;
            shell.stop().animate(ani, ops.duration, ops.easing, function () {
                ops.ondone.call(self, inx, self.index);
                self.index = inx;
            });
            ops.onplay.call(self, inx, this.index);
            return this;
        },
        next: function () {
            return this.play();
        },
        prev: function () {
            return this.play(this.index - 1);
        },
        autoPlay: function () {
            var 
			self = this;
            this._timer = setTimeout(function () {
                self.play().autoPlay();
            }, this.ops.delay);
        },
        stopAuto: function () {
            window.clearTimeout(this._timer);
        }
    };
})();



function check_imgNum() {
    var num = $('#J_imageView').children('img');
    return num.length;
}


//导航栏
function show_menu(flag) {
    if (flag == 1) {
        document.getElementById('type_menu').style.display = 'block';
    } 
else {
        document.getElementById('type_menu').style.display = 'none';
    }
}